﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tools.Linq;
using MobiMenu.Restaurant.Web.Utility;
using System.Collections.Specialized;
using System.Linq.Expressions;

namespace MobiMenu.Restaurant.Web
{
    /// <summary>
    ///Class1 的摘要说明
    /// </summary>
    public class AdvertisementsService : BaseRESTService<Advertisement>
    {
        public AdvertisementsService()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        protected override Expression<Func<Advertisement, bool>> GetWhereExpression(NameValueCollection nameValues)
        {
            var queryBuilder = QueryBuilder.Create<Advertisement>();
            
            int[] ids;
            if (RequestHelper.GetInt32Array(nameValues, "Id", out ids))
            {
                queryBuilder = queryBuilder.In(c => c.Id, ids);
            }

            int[] relatedDishIds;
            if (RequestHelper.GetInt32Array(nameValues, "RelatedDishId", out relatedDishIds))
            {
                queryBuilder = queryBuilder.In(c => c.RelatedDishId, Array.ConvertAll(relatedDishIds, CommonHelper.CovertToInt32Null));
            }

            bool showInMenu;
            if (RequestHelper.GetBool(nameValues, "ShowInMenu", out showInMenu))
            {
                queryBuilder = queryBuilder.Equals(c => c.ShowInMenu, showInMenu);
            }

            string title;
            if (RequestHelper.GetString(nameValues, "Title", out title))
            {
                queryBuilder = queryBuilder.Like(c => c.Title, title);
            }

            string content;
            if (RequestHelper.GetString(nameValues, "Content", out content))
            {
                queryBuilder = queryBuilder.Like(c => c.Content, content);
            }

            return queryBuilder.Expression;
        }

        protected override bool ProcessOrder(string sortField, bool isDesc, ref IQueryable<Advertisement> query)
        {
            if (string.IsNullOrEmpty(sortField))
            {
                return false;
            }

            if (sortField.Equals("Id", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Advertisement, int>> expr = c => c.Id;
                query = CommonHelper.QueryOrder<Advertisement, int>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("Content", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Advertisement, string>> expr = c => c.Content;
                query = CommonHelper.QueryOrder<Advertisement, string>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("Title", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Advertisement, string>> expr = c => c.Title;
                query = CommonHelper.QueryOrder<Advertisement, string>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("RelatedDishId", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Advertisement, int?>> expr = c => c.RelatedDishId;
                query = CommonHelper.QueryOrder<Advertisement, int?>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("ShowInMenu", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Advertisement, bool>> expr = c => c.ShowInMenu;
                query = CommonHelper.QueryOrder<Advertisement, bool>(query, expr, isDesc);
                return true;
            }
            
            return false;
        }

        protected override bool GetDeleteWhereExpression(RESTContext<Advertisement> context,
            out Expression<Func<Advertisement, bool>> expr)
        {
            expr = null;
            if (context == null || context.RequestData == null
                || context.RequestData.DeleteIds == null || context.RequestData.DeleteIds.Count == 0)
            {
                return false;
            }

            var queryBuilder = QueryBuilder.Create<Advertisement>();
            queryBuilder = queryBuilder.In(c => c.Id, context.RequestData.DeleteIds.ToArray());
            expr = queryBuilder.Expression;
            return true;
        }

        protected override bool UpdateEntity(NameValueCollection nameValues, Advertisement item)
        {
            bool changed = false;
            string content;
            if (RequestHelper.GetString(nameValues, "Content", out content))
            {
                item.Content = content;
                changed = true;
            }

            int relatedDishId;
            if (RequestHelper.GetInt32(nameValues, "RelatedDishId", out relatedDishId))
            {
                item.RelatedDishId = relatedDishId;
                changed = true;
            }

            string title;
            if (RequestHelper.GetString(nameValues, "Title", out title))
            {
                item.Title = title;
                changed = true;
            }
            
            bool showInMenu;
            if (RequestHelper.GetBool(nameValues, "ShowInMenu", out showInMenu))
            {
                item.ShowInMenu = showInMenu;
                changed = true;
            }

            return changed;
        }

        protected override bool GetUpdateWhereExpression(NameValueCollection nameValues, out Expression<Func<Advertisement, bool>> expr)
        {
            var queryBuilder = QueryBuilder.Create<Advertisement>();
            int id;
            if (RequestHelper.GetInt32(nameValues, "Id", out id))
            {
                queryBuilder = queryBuilder.Equals(c => c.Id, id);
                expr = queryBuilder.Expression;
                return true;
            }

            expr = null;
            return false;
        }
    }
}